//
//  WarViewController.m
//  WHSport
//
//  Created by ccjoy-jimneylee on 15/4/15.
//  Copyright (c) 2015年 jimneylee. All rights reserved.
//

#import "WarViewController.h"
#import "Util.h"
#import <UIImageView+AFNetworking.h>
#import "BusinessModel.h"
#import "LNWaterfallFlowLayout.h"
#import "LNWaterfallFlowFooterView.h"
#import "WarEntity.h"
#import "WarDetailViewC.h"
#import "UserModel.h"

@interface WarColCell : UICollectionViewCell
@property (nonatomic, weak) IBOutlet UIImageView *img;
@property (nonatomic, weak) IBOutlet UILabel *placeLab;
@property (nonatomic, weak) IBOutlet UILabel *typetitLab;
@property (nonatomic, weak) IBOutlet UILabel *timeLab;
@property (nonatomic, weak) IBOutlet UIView *backView;
@end

@implementation WarColCell
@end

@interface WarViewController (){
    NSArray *dataArraiy;
}
// 商品列表数组
@property (nonatomic, strong) NSMutableArray *goodsList;
// 当前的数据索引
@property (nonatomic, assign) NSInteger index;
// 瀑布流布局
@property (weak, nonatomic) IBOutlet LNWaterfallFlowLayout *waterfallFlowLayout;
// 底部视图
@property (nonatomic, weak) LNWaterfallFlowFooterView *footerView;

@end

@implementation WarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getData];
    self.navigationItem.title =@"约战";
    
    // Do any additional setup after loading the view.
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
}

- (void)getData{
    
    NSString *gpage =@"1";
    [[BusinessModel sharedInstance] getDatawithpage:gpage data:^(id info) {
        NSArray *array = [info copy];
        dataArraiy = array;
        self.waterfallFlowLayout.columnCount = 2;
        self.waterfallFlowLayout.goodsList = dataArraiy;
        [self.collectionView reloadData];
    }];
}

#pragma mark <UICollectionViewDataSource>

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return dataArraiy.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    WarColCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"WarCellID"
                                  forIndexPath:indexPath];
    WarEntity *warEty = dataArraiy[indexPath.row];
    NSString *str = [NSString stringWithFormat:@"http://218.93.12.178:8080/hhw-api/%@",warEty.picPath];
    NSURL *url = [NSURL URLWithString:str];
    [cell.img setImageWithURL:url];
    cell.placeLab.text = [NSString stringWithFormat:@"场馆:%@",
                          warEty.sportsHallName];
    cell.timeLab.text = [NSString stringWithFormat:@"时间:%@",
                         warEty.publishTimeStr];
    cell.typetitLab.text = [NSString stringWithFormat:@"项目:%@",
                            warEty.sportEventName];
    [cell bringSubviewToFront:cell.backView];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if ([UserModel sharedInstance].currentUser == nil) {
        [Util presentLoginControllerFromViewController:self];
    }
    
    else{
    
    WarEntity *warEty = dataArraiy[indexPath.row];
    UIStoryboard *main = [UIStoryboard storyboardWithName:@"Main"
                                                   bundle:nil];
    WarDetailViewC *warvc = [main instantiateViewControllerWithIdentifier:@"WarDetailViewC"];
    NSString *idNub = [NSString stringWithFormat:@"%@",
                       warEty.warid];
    warvc.warID = idNub;
    [self.navigationController pushViewController:warvc animated:YES];
        
    }

}

@end
